﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Common.Contract.DataCenter.Record;
using Common.Service.Queue;
using Common.Type;
using DataCenter.ServiceCommand;
using System.Messaging;

namespace DataCenter.Test
{
    [TestClass]
    public class GetQueueAndUpdateDB_Test : TestBase
    {
        private MessageQueue messageQueue = null;
        private bool isRunning = false;
        [TestMethod]
        public void GetQueueAndUpdateDB_Success()
        {
            InitializeQueue();

            System.Threading.Thread.Sleep(10000);
            Assert.IsTrue(isRunning);
        }


        #region Helper functions
        private void InitializeQueue()
        {
            messageQueue = QueueFactory.Queue;
            isRunning = true;
            messageQueue.Formatter = new BinaryMessageFormatter();
            messageQueue.ReceiveCompleted += OnReceiveCompleted;
            messageQueue.BeginReceive();
        }

        private int receivedCounter;
        private void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
        {
            try
            {
                MessageQueue mq = (MessageQueue)source;
                if (mq != null)
                {
                    try
                    {
                        System.Messaging.Message message = null;
                        try
                        {
                            message = mq.EndReceive(asyncResult.AsyncResult);
                        }
                        catch (Exception ex)
                        {
                            //LogMessage(ex.Message);
                        }
                        if (message != null)
                        {
                            IMessageQueueRecord queue = message.Body as IMessageQueueRecord;
                            if (queue != null)
                            {
                                receivedCounter++;
                                if (receivedCounter == int.MaxValue) receivedCounter = 0;

                                switch (queue.MessageType)
                                {
                                    case (int)SyncType.REGION:
                                        new UpdateRegionServiceCommand(this.Provider).Execute(((SyncRegionRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.PROVINCE:
                                        new UpdateProvinceServiceCommand(this.Provider).Execute(((SyncProvinceRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.DISTRICT:
                                        new UpdateDistrictServiceCommand(this.Provider).Execute(((SyncDistrictRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.SHOP_INFOMATION:
                                        new UpdateShopServiceCommand(this.Provider).Execute(((SyncShopInfoRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.SHOP_CELL:
                                        new UpdateShopCellServiceCommand(this.Provider).Execute(((SyncShopCellRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.MODEL_CAT:
                                        new UpdateModelCatServiceCommand(this.Provider).Execute(((SyncModelCatRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.CDC_HISTORY:
                                        new UpdateCdcHistoryServiceCommand(this.Provider).Execute(((SyncCdcHistRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.PND_HISTORY:
                                         new UpdatePndHistoryServiceCommand(this.Provider).Execute(((SyncPndHistRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.RDC_HISTORY:
                                        new UpdateRdcHistoryServiceCommand(this.Provider).Execute(((SyncRdcHistRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.WARRANTY_HISTORY:
                                        new UpdateWarrantyHistoryServiceCommand(this.Provider).Execute(((SyncWarrantyHistoryRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.WARRANTY_MASTER:
                                        new UpdateWarrantyMasterServiceCommand(this.Provider).Execute(((SyncWarrantyMasterRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.WARRANTY_MESSAGE:
                                        new UpdateWarrantyMessageServiceCommand(this.Provider).Execute(((SyncWarrantyMessageRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.BILLING_INFOMATION:
                                        new UpdateBillingInfoServiceCommand(this.Provider).Execute(((SyncBillingInfoRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.SHOP_BILL_RELATION:
                                        new UpdateShopBilltoServiceCommand(this.Provider).Execute(((SyncShopBilltoRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.MRP:
                                        new UpdateMrpServiceCommand(this.Provider).Execute(((SyncMrpRecord)message.Body).Record);
                                        break;
                                    case (int)SyncType.ORDER:
                                        new UpdateOrderServiceCommand(this.Provider).Execute(((SyncOrderRecord)message.Body).Record);
                                        break;
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (isRunning)
                        {
                            mq.BeginReceive();
                        }
                    }
                }
                return;
            }
            catch (Exception exc)
            {
                //LogMessage(exc.Message);
            }
        }
        #endregion
    }
}
